package com.wf.atm.pojo;

import lombok.Data;
import com.wf.atm.exception.ATMException;
import com.wf.atm.exception.BalanceNotEnoughException;

import java.io.Serializable;

/**
 * @author mpk
 * @since 2024/6/4 14:52:43
 */

@Data
public class LoanSavingAccount extends SavingAccount implements Loanable, Serializable {


    private static final long serialVersionUID = 4073796806370851024L;
    private long loanAmount;
    @Override
    public Account requestLoan(double money) throws BalanceNotEnoughException, ATMException {

        if(money<0)
        {
            throw  new BalanceNotEnoughException("贷款金额不能为负数！");
        }

        this.loanAmount+= (long) money;
        this.deposit(money);
        return this;
    }

    public LoanSavingAccount(Long id, String password, String name, String personId, String address,String email) {
        super(id, password, name, personId,address, email);
    }

    @Override
    public Account payLoan(double money) throws ATMException {


        if(this.loanAmount<money){

            throw  new ATMException("还款金额过多！");
        }
        this.loanAmount-= (long) money;
        try {
            this.withdraw(money);
        } catch (BalanceNotEnoughException e) {
            System.out.println(e.getMessage());
        }
        return this;
    }

    @Override
    public String toString() {
        return "LoanSavingAccount{" +
                "loanAmount=" + loanAmount +
                "} " + super.toString();
    }



}
